import com.ithema.entity.Books;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;

import java.util.EnumSet;

public class testhibernate {

    @Test
    public  void  fn1(){
        Configuration cfg = new Configuration().configure("myhib.cfg.xml");
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
       // session.beginTransaction();
        Books books = new Books();
        books.setBname("《品三国》");
        books.setBprices(34.34);
        session.save(books);
        //session.getTransaction().commit();
        session.close();
        sessionFactory.close();
        /*@Data
        @Entity
        public class Books {
                @Id @GeneratedValue
                private  Integer bid;
                private  String bname;
                private  Double  bprices;
                }*/

        /*Hibernate:
        create table Books (
           bid integer not null,
            bname varchar(255),
            bprices double precision,
            primary key (bid)
        ) engine=InnoDB
        Hibernate:

            create table hibernate_sequence (
               next_val bigint
            ) engine=InnoDB
        Hibernate:
            insert into hibernate_sequence values ( 1 )
        Hibernate:
            select
                next_val as id_val
            from
                hibernate_sequence for update
        Hibernate:
            update
                hibernate_sequence
            set
                next_val= ?
            where
                next_val=?
        Hibernate:
            insert
            into
                Books
                (bname, bprices, bid)
            values
                (?, ?, ?)*/
            }
    @Test
    public  void  fn2(){
        StandardServiceRegistryBuilder configure = new StandardServiceRegistryBuilder().configure("myhib.cfg.xml");
        StandardServiceRegistry build = configure.build();
        Metadata metadata = new MetadataSources(build).buildMetadata();
        SchemaExport ext = new SchemaExport();
        //输出格式化
        ext.setFormat(true);

        //格式化输出  输出到控制台
        ext.setFormat(true);
        ext.create(EnumSet.of(TargetType.STDOUT), metadata);


        //格式化输出到本地磁盘文件
        //ext.setFormat(true);
        //ext.setOutputFile("c:/test.sql");
        //ext.create(EnumSet.of(TargetType.SCRIPT), metadata);

        //格式化输出到本地磁盘项目中文件
        //ext.setFormat(true);
        //ext.setOutputFile("test.sql");
       // ext.create(EnumSet.of(TargetType.SCRIPT), metadata);



        //ext.create(EnumSet.of(TargetType.DATABASE), metadata);



    }

}
